package tulingxueyuan117;

import java.util.Arrays;

/**
 * Author: Zhang Dongwei
 * Date: 2022/11/6 14:52
 * 选择升序排序
 */
public class p33_选择排序_912 {
    public static void main(String[] args) {
        int[] arr = {86,39,77,23,32,45,58,63,93,4,37,22};
        System.out.println(Arrays.toString(arr));
        System.out.println("=========================");
        int[] res = choiceSort(arr);
        System.out.println("=========================");
        System.out.println(Arrays.toString(res));
    }

//    每轮选择最小的放在最前面，在力扣912会超时
    public static int[] choiceSort(int[] arr) {
        for(int i=0; i<arr.length; i++){
            int min = arr[i];
            int minindex = i;
            for(int j=i+1; j<arr.length; j++){
                if(arr[j]<min){
                    min = arr[j];
                    minindex = j;
                }
            }
            int temp = arr[i];
            arr[i] = arr[minindex];
            arr[minindex] = temp;
            System.out.println(Arrays.toString(arr));
        }
        return arr;
    }

}
